/**
 * 代码编码与代码提示管理
 */
// 错误编码
const SUCCESS_CODE = 10000
const AUTH_FAIL_CODE = 10001
const NO_PERMISSION_CODE = 10002
const PARAM_INVALIDATE_CODE = 10003
const ACCOUNT_OR_PASSWORD_ERROR_CODE = 10004
const NOT_LOGIN_ERROR_CODE = 10005
const TOKEN_EXPIRE_ERROR_CODE = 10006
const ACCOUNT_DISABLE_ERROR_CODE = 10007
const ACCOUNT_EXISTS_ERROR_CODE = 10008
const DECRYPT_ERROR_CODE = 10009
const PROJECT_EXISTS_ERROR_CODE = 10010
const PROJECT_NOT_EXISTS_ERROR_CODE = 10011
const ACCOUNT_NOT_EXISTS_ERROR_CODE = 10012
const PROJECT_CODE_NOT_EXISTS_ERROR_CODE = 10013
const UNKNOW_CODE = 99998
const SYS_ERROR_CODE = 99999

// 错误编码对应的错误信息
const ERR_MSG_MAP = {
  [SUCCESS_CODE]: '操作成功',
  [AUTH_FAIL_CODE]: '认证失败',
  [NO_PERMISSION_CODE]: '无权限',
  [PARAM_INVALIDATE_CODE]: '参数错误',
  [ACCOUNT_OR_PASSWORD_ERROR_CODE]: '账号或密码错误',
  [NOT_LOGIN_ERROR_CODE]: '未登录,请先登录',
  [TOKEN_EXPIRE_ERROR_CODE]: 'token已过期,请重新登录',
  [ACCOUNT_DISABLE_ERROR_CODE]: '账号已禁用',
  [ACCOUNT_EXISTS_ERROR_CODE]: '账号已存在',
  [DECRYPT_ERROR_CODE]: '解密失败',
  [PROJECT_EXISTS_ERROR_CODE]: '项目已存在',
  [ACCOUNT_NOT_EXISTS_ERROR_CODE]: '账号不存在',
  [PROJECT_NOT_EXISTS_ERROR_CODE]: '项目不存在',
  [PROJECT_CODE_NOT_EXISTS_ERROR_CODE]: '项目编码不存在',
  [UNKNOW_CODE]: '未知错误',
  [SYS_ERROR_CODE]: '系统内部错误',
}

// 自定义错误需要继承 Error
class CustomErr extends Error {
  code = ''
  msg = ''
  data = ''
  stack = ''
  constructor(code, msg = null, data = null) {
    super(msg)
    this.code = code
    this.msg = msg ? msg : ERR_MSG_MAP[code] || ERR_MSG_MAP[UNKNOW_CODE] // 如果不存在code则默认为未知错误
    this.data = data
  }
  //
  error() {
    return {
      code: this.code,
      msg: this.msg,
      data: this.data,
    }
  }
  // 格式化为str
  toString() {
    return JSON.stringify(this.error())
  }
}

class SysErr extends CustomErr {
  constructor(data, stack = null) {
    super(SYS_ERROR_CODE, ERR_MSG_MAP[data], data)
    this.stack = stack
  }
  error() {
    return {
      code: this.code,
      msg: this.msg,
      data: this.data,
      stack: this.stack,
    }
  }
}
// 成功
class Success extends CustomErr {
  // data 可为 {} 或 [] 或 string
  constructor(data = {}) {
    super(SUCCESS_CODE, ERR_MSG_MAP[SUCCESS_CODE], data)
  }
}

// 认证授权错误
const AuthErr = new CustomErr(AUTH_FAIL_CODE)

// 无权限错误
const NoPermissionErr = new CustomErr(NO_PERMISSION_CODE)

// 传入参数错误
const ParamInvalidErr = new CustomErr(PARAM_INVALIDATE_CODE)

// 账号或密码错误
const AccountOrPasswordErr = new CustomErr(ACCOUNT_OR_PASSWORD_ERROR_CODE)
// 未登录错误
const NotLoginErr = new CustomErr(NOT_LOGIN_ERROR_CODE)
//
const TokenExpireErr = new CustomErr(TOKEN_EXPIRE_ERROR_CODE)
//
const AccountDisabledErr = new CustomErr(ACCOUNT_DISABLE_ERROR_CODE)
//
const AccountExistsErr = new CustomErr(ACCOUNT_EXISTS_ERROR_CODE)
// 账号不存在
const AccountNotExistsErr = new CustomErr(ACCOUNT_NOT_EXISTS_ERROR_CODE)
// 项目已存在
const ProjectExistsErr = new CustomErr(PROJECT_EXISTS_ERROR_CODE)
const ProjectNotExistsErr = new CustomErr(PROJECT_NOT_EXISTS_ERROR_CODE)
const ProjectCodeNotExistErr = new CustomErr(PROJECT_CODE_NOT_EXISTS_ERROR_CODE)
const DecryptErr = new CustomErr(DECRYPT_ERROR_CODE)

const UnknowErr = new CustomErr(UNKNOW_CODE)

module.exports = {
  Success,
  CustomErr,
  SysErr,
  AccountOrPasswordErr,
  AccountDisabledErr,
  AccountExistsErr,
  AccountNotExistsErr,

  DecryptErr,
  NotLoginErr,
  TokenExpireErr,
  ProjectExistsErr,
  ProjectNotExistsErr,
  ProjectCodeNotExistErr,

  AuthErr,
  NoPermissionErr,
  ParamInvalidErr,
  UnknowErr,
}
